if SERVER then
	AddCSLuaFile("shared.lua")
end

SWEP.HoldType = "ar2"

if CLIENT then
	SWEP.PrintName = "M16A2"
	SWEP.Slot = 2
	SWEP.ViewModelFlip = false
end

SWEP.Base = "weapon_gun_base"
SWEP.Primary.Recoil	= 1
SWEP.Primary.Damage = 15
SWEP.Primary.Delay = 0.75
SWEP.Primary.MaxCone = 0.025
SWEP.Primary.Cone = 0.03
SWEP.Primary.MinCone = 0.003
SWEP.Primary.ClipSize = 30
SWEP.Primary.Automatic = true
SWEP.Primary.DefaultClip = 30
SWEP.Primary.ClipMax = 60
SWEP.Primary.Ammo = "AR2"
SWEP.Knockback = 375

SWEP.ViewModel = "models/weapons/v_rif_m16a2.mdl"
SWEP.WorldModel	= "models/weapons/w_rif_m4a1.mdl"

SWEP.Primary.Sound = Sound("Weapon_M4A1.Single")

SWEP.IronSightsPos = Vector(-2.651, -2.132, 0.8)
SWEP.IronSightsAng = Vector(0, 0, 0)

SWEP.Shots = 3
SWEP.Burst = 0
SWEP.BurstDelay = 0.07

function SWEP:PrimaryAttack()
	local owner = self.Owner
	local wep = self.Weapon

	local cooldown = math.Clamp(CurTime() - self:GetLastShootTime(), 0, 2)/2
	
	self.Primary.RecCone = Lerp(cooldown, self.Primary.MaxCone, self.Primary.MinCone)
	
	-- wep:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	wep:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
	
	if not self:CanPrimaryAttack() then return end
	
	self.Burst = self.Shots - 1
	wep:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	
	self:EmitSound(self.Primary.Sound, self:GetPos(), 100)
	
	self:ShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self:GetPrimaryCone())
	self:TakePrimaryAmmo(1)
	
	owner:ViewPunch(Angle((math.Rand(-0.2,-0.5) * self.Primary.Recoil) * self.ConeAffect, (math.Rand(-0.2,0.5) * self.Primary.Recoil) * self.ConeAffect, 0))
		
	self:SetLastShootTime(CurTime())
end

function SWEP:SecondaryThink()
	if self:GetLastShootTime() + self.BurstDelay < CurTime() then
		local owner = self.Owner
		
		if self.Burst > 0 then
			self.Burst = self.Burst - 1
			self:SetLastShootTime(CurTime())
				
			if self:CanPrimaryAttack() then
				self:EmitSound(self.Primary.Sound, self:GetPos(), 100)
	
				self:ShootBullet(self.Primary.Damage, self.Primary.Recoil * 0.25, self.Primary.NumShots, self:GetPrimaryCone())
				self:TakePrimaryAmmo(1)

				owner:ViewPunch(Angle((0.1 * self.Primary.Recoil) * self.ConeAffect, (math.Rand(-0.1,0.3) * self.Primary.Recoil) * self.ConeAffect, 0))
			end
		end
	end
end